package sparksql.say8.lesson04

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

/**
  * Created by Administrator on 2018/5/4.
  */
case class Person(var name:String,var age:Long)
object DataSetTest {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("test")
      .master("local")
      .getOrCreate()

    import spark.implicits._
    /**
      * RDD
      */
    val rdd: RDD[Person] = spark.sparkContext.textFile("D:\\1711班\\第十三天\\资料\\people.txt")
      .map(line => Person(line.split(",")(0), line.split(",")(1).trim.toLong))

    rdd.filter( person =>{ //函数式编程
      if(person.age > 28){//面向对象编程
        true
      }else{
        false
      }
    }).foreach( person =>{
      println(person.name + " "+ person.age)
    })
    /**
      * DataFrame
      *
      */

    val df: DataFrame = spark.read.json("D:\\1711班\\第十三天\\资料\\resources\\people.json")

    df.createOrReplaceTempView("people")
    //SQL方式去编程
    spark.sql("select * from people where age > 28").show()

    /**
      *
      * RDD -》 DataFrame
      *   DataFrame  比 RDD 多了 schema的信息
      * DataFrame -> DataSet
      *  DataSet 比 Dataframe多了
      *
      */

    val ds: Dataset[Person] = spark.read.json(
      "D:\\1711班\\第十三天\\资料\\resources\\people.json").as[Person]
    //方式一
    ds.filter( person =>{
      if(person.age > 28){
        true
      }else{
        false
      }
    }).foreach( person =>{
      //xxx
    })
    //方式二
    ds.createOrReplaceTempView("people")
    spark.sql("select * from people where age > 28")








  }

}
